package com.tuniu.agents.util;

import com.tuniu.agents.common.I18nService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Locale;

/**
 * Internationalization Utility Class
 * Provides utility methods for accessing internationalized messages.
 * This class is a wrapper around I18nService to provide static access to i18n features.
 */
@Slf4j
@Component
public class I18nUtil {

    private static I18nService i18nService;

    /**
     * Set the I18nService instance
     * @param i18nService the internationalization service to use
     */
    @Autowired
    public void setMessageSource(I18nService i18nService) {
        I18nUtil.i18nService = i18nService;
    }

    /**
     * Get internationalized message
     * Retrieves a message for the default locale with no arguments.
     *
     * @param code message code
     * @return internationalized message
     */
    public static String getMessage(String code) {
        return getMessage(code, (Object[]) null);
    }

    /**
     * Get internationalized message
     * Retrieves a message for the default locale with the specified arguments.
     *
     * @param code message code
     * @param args parameters
     * @return internationalized message
     */
    public static String getMessage(String code, Object[] args) {
        return getMessage(code, args, "");
    }

    /**
     * Get internationalized message
     * Retrieves a message for the default locale with specified arguments and default message.
     *
     * @param code           message code
     * @param args           parameters
     * @param defaultMessage default message
     * @return internationalized message
     */
    public static String getMessage(String code, Object[] args, String defaultMessage) {
        return i18nService.getMessage(code, args, defaultMessage);
    }

    /**
     * Get internationalized message for a specific locale
     * Retrieves a message for the specified locale with no arguments.
     *
     * @param code   message code
     * @param locale the locale to use
     * @return internationalized message
     */
    public static String getMessage(String code, Locale locale) {
        return getMessage(code,  null, locale);
    }

    /**
     * Get internationalized message for a specific locale
     * Retrieves a message for the specified locale with the specified arguments.
     *
     * @param code   message code
     * @param args   parameters
     * @param locale the locale to use
     * @return internationalized message
     */
    public static String getMessage(String code, Object[] args, Locale locale) {
        return getMessage(code, args, "", locale);
    }

    /**
     * Get internationalized message for a specific locale
     * Retrieves a message for the specified locale with arguments and default message.
     *
     * @param code           message code
     * @param args           parameters
     * @param defaultMessage default message
     * @param locale         the locale to use
     * @return internationalized message
     */
    public static String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        return i18nService.getMessage(code, args, defaultMessage, locale);
    }

    /**
     * Get internationalized message for a specific conversation
     * Retrieves a message for the conversation's locale with no arguments.
     *
     * @param conversationId conversation id
     * @param code           message code
     * @return internationalized message
     */
    public static String getMessage(String conversationId, String code) {
        return getMessage(conversationId, code, null);
    }

    /**
     * Get internationalized message for a specific conversation
     * Retrieves a message for the conversation's locale with specified arguments.
     *
     * @param conversationId conversation id
     * @param code           message code
     * @param args           parameters
     * @return internationalized message
     */
    public static String getMessage(String conversationId, String code, Object[] args) {
        return getMessage(conversationId, code, args, "");
    }

    /**
     * Get internationalized message for a specific conversation
     * Retrieves a message for the conversation's locale with specified arguments and default message.
     *
     * @param conversationId conversation id
     * @param code message code
     * @param args parameters
     * @param defaultMessage default message
     * @return internationalized message
     */
    public static String getMessage(String conversationId, String code, Object[] args, String defaultMessage) {
        return i18nService.getMessageForConversation(conversationId, code, args, defaultMessage);
    }

    /**
     * Get the default locale
     * @return the default locale from the i18n service
     */
    public static Locale getLocale() {
        return i18nService.getLocale();
    }

    /**
     * Get the locale for a specific conversation
     * @param localeContextId the conversation ID
     * @return the locale for the specified conversation
     */
    public static Locale getLocaleForConversation(String localeContextId) {
        return i18nService.getLocaleForConversation(localeContextId);
    }

    /**
     * Set the locale for a specific conversation
     * @param conversationId the conversation ID
     * @param locale the locale to set
     */
    public static void setLocaleForConversation(String conversationId, Locale locale) {
        i18nService.setLocaleForConversation(conversationId, locale);
    }

    /**
     * Get a locale from a language tag
     * @param languageTag the language tag (e.g., "en-US", "zh-CN")
     * @return the locale corresponding to the language tag, or the default locale if no tag provided
     */
    public static Locale getLocaleWithLanguageTag(String languageTag) {
        if (StringUtils.hasText(languageTag)) {
            return Locale.forLanguageTag(languageTag);
        }
        return getLocale();
    }

    /**
     * Get the language tag for a specific locale context
     * @param localeContextId the locale context ID
     * @return the language tag for the context's locale
     */
    public static String getLocaleLanguageTag(String localeContextId) {
        return i18nService.getLocale(localeContextId).toLanguageTag();
    }

    public static String getCurMessage(String code, Object... args) {
        return get(code, args != null ? args : new Object[0]);
    }

    public static String getCurMessage(String code) {
        return get(code, new Object[0]);
    }

    public static String get(String code, Object[] args) {
        return i18nService.getMessage("", code, args, "");
    }

}